<template>
    <div class="table">
        <el-breadcrumb separator="/" class="breadCrumb">
            <el-breadcrumb-item>{{$t('sidebar.listTaskMsg')}}</el-breadcrumb-item>
            <el-breadcrumb-item>{{$t('sidebar.taskRunnerLog')}}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="crumbs">
            <el-row :gutter="15">
                <el-col :span="2" style="min-width:100px;">
                    <el-input
                        :placeholder="$t('taskRunnerLog.pleaseInput',{ name: 'ID' })"
                        v-model="searchForm.id">
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-input
                        :placeholder="$t('taskRunnerLog.pleaseInput',{ name: $t('taskRunnerLog.taskClass') })"
                        v-model="searchForm.taskClass">
                    </el-input>
                </el-col>
                <el-col :span="6" style="min-width:345px;">
                    <el-date-picker
                        v-model="queueDate"
                        type="datetimerange"
                        align="right"
                        :picker-options="pickerOptions"
                        :placeholder="$t('taskRunnerLog.pleaseInput',{ name: $t('taskRunnerLog.queueDate') })" style="width:100%;">
                    </el-date-picker>
                </el-col>
                <el-col :span="2" style="min-width:160px;">
                    <el-select v-model="searchForm.status" clearable :placeholder="$t('taskRunnerLog.pleaseInput',{ name: $t('taskRunnerLog.status') })">
                        <el-option key="0" :label="$t('taskRunnerLog.notSet')" value="0"></el-option>
                        <el-option key="1" :label="$t('taskRunnerLog.success')" value="1"></el-option>
                        <el-option key="2" :label="$t('taskRunnerLog.programErr')" value="2"></el-option>
                        <el-option key="3" :label="$t('taskRunnerLog.misconfiguration')" value="3"></el-option>
                        <el-option key="4" :label="$t('taskRunnerLog.thirdErr')" value="4"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="4" style="line-height: 35px;">
                    <el-button size="small" type="primary" @click="searchData()">{{$t('taskRunnerLog.search')}}</el-button>
                    <el-button size="small" type="primary" @click="refreshData()">{{$t('taskRunnerLog.refresh')}}</el-button>
                </el-col>
            </el-row>
        </div>
        <el-table class="tableScroll" :data="tableData" border style="width: 100%" :max-height="$store.state.allTable.tableHeight" v-loading="tableDataLoading"
                  :element-loading-text="$t('allTable.dataLoading')">
            <el-table-column prop="id" label="ID" min-width="80" fixed>
            </el-table-column>
            <el-table-column prop="refId" :label="$t('taskRunnerLog.refId')" min-width="60">
            </el-table-column>
            <el-table-column prop="taskClass" :label="$t('taskRunnerLog.taskClass')" min-width="150">
            </el-table-column>
            <el-table-column prop="taskParam" :label="$t('taskRunnerLog.taskParam')" min-width="100">
            </el-table-column>
            <el-table-column prop="runType" :label="$t('taskRunnerLog.runType')" width="100" :formatter="runTypeShow">
            </el-table-column>
            <el-table-column prop="runTarget" :label="$t('taskRunnerLog.runTarget')" min-width="100">
            </el-table-column>
            <el-table-column prop="hostIp" :label="$t('taskRunnerLog.hostIp')" min-width="80">
            </el-table-column>
            <el-table-column prop="hostId" :label="$t('taskRunnerLog.hostId')" min-width="80">
            </el-table-column>
            <el-table-column prop="taskConfig" :label="$t('taskRunnerLog.taskConfig')" min-width="100">
            </el-table-column>
            <el-table-column prop="queueDate" :label="$t('taskRunnerLog.queueDate')" width="80">
            </el-table-column>
            <el-table-column prop="consumeDate" :label="$t('taskRunnerLog.consumeDate')" width="80">
            </el-table-column>
            <el-table-column prop="runDate" :label="$t('taskRunnerLog.runDate')" width="80">
            </el-table-column>
            <el-table-column prop="finishDate" :label="$t('taskRunnerLog.finishDate')" width="80">
            </el-table-column>
            <el-table-column :label="$t('taskRunnerLog.resultData')" width="100">
                <template scope="scope">
                    <p class="resultData ellipsis">{{ scope.row.resultData }}</p>
                </template>
            </el-table-column>
            <el-table-column prop="ranTimes" :label="$t('taskRunnerLog.ranTimes')" width="45">
            </el-table-column>
            <el-table-column prop="status" :label="$t('taskRunnerLog.status')" width="60" :formatter="statusShow">
            </el-table-column>
        </el-table>
        <div class="mgtb20" :class="{ pagination : tableData.length !== 0}">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change ="handleCurrentChange"
                :page-sizes="$store.state.allTable.pageSizesChoice"
                :page-size="pageSizeGetter"
                layout="total, sizes, prev, pager, next, jumper"
                :total="sizeAll"
                :page-count="pageCount"
                v-if="tableData.length !== 0">
            </el-pagination>
        </div>
    </div>
</template>
<script>
    import { mapGetters,mapMutations } from 'vuex';
    import common from '../../assets/script/common';
    export default {
        data() {
            return {
                tableData:[],
                pageName:'taskRunnerLog',
                pageCount:1,
                sizeAll:0,
                dialogFormVisible: false,
                hasTaskClass: true,
                dialogType:'',
                queueDate:['',''],
                searchForm:{
                    id:'',
                    status:'',
                    taskClass:'',
                    startQueueDate:'',
                    endQueueDate:''
                },
                tableDataLoading:true,
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                        }
                    }]
                }
            }
        },
        created() {
            this.getData(1,this.$store.state.allTable.pageSize);
        },
        computed:{
            ...mapGetters([
                "pageSizeGetter",
            ]),
        },
        methods:{
            ...mapMutations([
                'pageSizeMutation'
            ]),
            handleSizeChange(val) {
                let self = this;
                self.pageSizeMutation(val);
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            handleCurrentChange(val) {
                common.changePageGoToTop(this,val);
            },
            getData(_pageSize, _pageNum) {
                let self = this;
                self.tableDataLoading = true;
                common.getTableList(this, 'taskRunnerLog', _pageSize, _pageNum, self.searchForm, (data) => {
                    self.tableData = data.results ? data.results : [];
                    self.pageCount = data.pageCount;
                    self.sizeAll = data.sizeAll;
                    self.tableDataLoading = false;
                });
            },
            searchData() {
                let self = this;
                if(self.queueDate[0] !== null && self.queueDate[1] !== null) {
                    self.searchForm.startQueueDate = self.queueDate[0];
                    self.searchForm.endQueueDate = self.queueDate[1];
                }else{
                    self.searchForm.startRunDate = '';
                    self.searchForm.endRunDate = '';
                }
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            refreshData() {
                let self = this;
                for(let key in self.searchForm){
                    self.searchForm[key] = '';
                }
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            addButton() {
                let __ruleForm = this.ruleForm;
                for(let key in __ruleForm){
                    __ruleForm[key] = '';
                }
                this.dialogFormVisible = true;
                this.dialogType = 'add';
                this.hasTaskClass = true;
            },
            statusShow(row, column) {
                if(row.status === 0)
                    return "未设置";
                else if(row.status === 1)
                    return "成功";
                else if(row.status === 2)
                    return "程序错误";
                else if(row.status === 3)
                    return "配置错误";
                else if(row.status === 4)
                    return "第三方接口错误";
            },
            runTypeShow(row, column) {
                switch (row.runType){
                    case 1:
                        return "本地运行";
                    case 3:
                        return "全局运行";
                    case 5:
                        return "全局运行RPC";
                }
            }
        }
    }
</script>
<style lang="stylus">
    .resultData
        height 24px
</style>
